package net.qhzw.vip.qiyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.Area;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.AreaService;
import net.qhzw.vip.qiyu.util.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
public class AreaController {

    @Autowired
    private AreaService areaService;

    /**
     * 分页获取
     */
    @GetMapping("p/area/listByPid")
    @ApiOperation(value = "获取省市区信息", notes = "根据省市区的pid获取地址信息")
    @ApiImplicitParam(name = "pid", value = "省市区的pid(pid为0获取所有省份)", required = true, dataType = "String")
    public JsonResult<List<Area>> plistByPid(Long pid) {
        if (pid == null) {
            pid = 0L;
        }
        List<Area> list = areaService.listByPid(pid);
        return JsonResult.ok(list);
    }


    /**
     * 分页获取
     */
    @GetMapping("admin/area/page")
    public JsonResult<IPage<Area>> page(Area area, PageParam<Area> page) {
        IPage<Area> sysUserPage = areaService.page(page, new LambdaQueryWrapper<Area>());
        return JsonResult.ok(sysUserPage);
    }

    /**
     * 获取省市
     */
    @GetMapping("admin/area/list")
    public JsonResult<List<Area>> list(Area area) {
        List<Area> areas = areaService.list(new LambdaQueryWrapper<Area>()
                .like(area.getAreaName() != null, Area::getAreaName, area.getAreaName()));
        return JsonResult.ok(areas);
    }

    /**
     * 通过父级id获取区域列表
     */
    @GetMapping("admin/area/listByPid")
    public JsonResult<List<Area>> listByPid(Long pid) {
        List<Area> list = areaService.listByPid(pid);
        return JsonResult.ok(list);
    }

    /**
     * 获取信息
     */
    @GetMapping("admin/area/info/{id}")
    public JsonResult<Area> info(@PathVariable("id") Long id) {
        Area area = areaService.getById(id);
        return JsonResult.ok(area);
    }

    /**
     * 保存
     */
    @PostMapping("admin/area")
    public JsonResult save(@Valid @RequestBody Area area) {
        if (area.getParentId() != null) {
            Area parentArea = areaService.getById(area.getParentId());
            area.setLevel(parentArea.getLevel() + 1);
            areaService.removeAreaCacheByParentId(area.getParentId());
        }
        areaService.save(area);
        return JsonResult.ok();
    }

    /**
     * 修改
     */
    @PutMapping("admin/area")
    public JsonResult update(@Valid @RequestBody Area area) {
        areaService.updateById(area);
        areaService.removeAreaCacheByParentId(area.getParentId());
        return JsonResult.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("admin/area/{id}")
    public JsonResult delete(@PathVariable Long id) {
        Area area = areaService.getById(id);
        areaService.removeById(id);
        areaService.removeAreaCacheByParentId(area.getParentId());
        return JsonResult.ok();
    }


}
